package com.slipper.system.ai.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.ai.dao.AiSupplierModelDao;
import com.slipper.system.ai.bo.AiSupplierModelBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class AiSupplierModelService {
    @Resource
    private AiSupplierModelDao aiSupplierModelDao;
    
    public AiSupplierModelBO queryBy(String id) {
        return aiSupplierModelDao.queryBy(id);
    }
    
    public List<AiSupplierModelBO> queryList(AiSupplierModelBO bo) {
        return aiSupplierModelDao.queryList(bo);
    }
    
    public PageInfo<AiSupplierModelBO> queryPage(AiSupplierModelBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(aiSupplierModelDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(AiSupplierModelBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return aiSupplierModelDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return aiSupplierModelDao.insert(bo);
        }
    }

    @WriteTransactional
    public void saveList(List<AiSupplierModelBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (aiSupplierModelDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                aiSupplierModelDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                aiSupplierModelDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public int delete(AiSupplierModelBO bo) {
        return aiSupplierModelDao.delete(bo);
    }

    @WriteTransactional
    public int deleteBySupplierId(String supplierId) {
        return aiSupplierModelDao.deleteBySupplierId(supplierId);
    }
}
